﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using One.Web;
using One.Framework;
using Money.Data;
using Money.Interfaces;

public partial class Admin_CurrencyManagement : System.Web.UI.Page
{
    #region fields

    private ICurrencyService currencyService;
    protected List<Currency> currencies;
    private CurrencyOrderBy orderBy;
    private OrderDirection orderDirection;
    private string redirectUrl = "CurrencyManagement.aspx";
    private int success;
    private string errorIds;
    private int totalRecords;

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        currencyService = ServiceFactory.Create<ICurrencyService>();

        GetParams();
        LoadData();
        if (!IsPostBack)
            UpdateView();
    }

    protected void DeleteCurrencies(object sender, EventArgs e)
    {
        string[] deleteIdString;
        string idError = "";
        if (string.IsNullOrEmpty(Request.Form["selectedItems"]))
        {
            return;
        }
        deleteIdString = Request.Form["selectedItems"].Split(',');
        for (int i = 0; i < deleteIdString.Length; i++)
        {
            int id = 0;
            try
            {
                if (!int.TryParse(deleteIdString[i], out id))
                    id = 0;
                if(currencyService.CanDelete(id))
                    currencyService.DeleteCurrency(id);
                else
                    idError = string.Format("{0},{1}", idError, id);
            }
            catch
            {
                if (id != 0)
                    idError = string.Format("{0},{1}", idError, id);
            }
        }
        if (!string.IsNullOrEmpty(idError))
        {
            idError = idError.TrimStart(',');
            Response.Redirect(string.Format("{0}?success={1}&idserror={2}", redirectUrl, -3, idError));
        }
        else
        {
            Response.Redirect(string.Format("{0}?success={1}", redirectUrl, 3));
        }
    }

    #endregion

    #region private functions

    private void GetParams()
    {
        orderBy = QueryStringUtility.GetEnum<CurrencyOrderBy>("OrderBy", CurrencyOrderBy.Id.ToString());
        orderDirection = QueryStringUtility.GetEnum<OrderDirection>("OrderDirection", OrderDirection.ASC.ToString());
        success = QueryStringUtility.GetInt32("success", 0);
        errorIds = QueryStringUtility.GetString("idserror", "");
    }

    private void LoadData()
    {
        string strOrderBy = orderBy.ToString();
        currencies = currencyService.GetCurrencies(orderBy.ToString(), orderDirection.ToString(), pager.PageIndex, MoneyManagerConfig.AdminPageSize, out totalRecords);
    }

    private void UpdateView()
    {
        repeaterCurrencies.DataSource = currencies;
        repeaterCurrencies.DataBind();

        (Master as Admin_MasterPage).SelectedTab = "currencyManagement";
        pageTitle.Text = Resources.Strings.CurrencyManagement;
        pager.TotalRecords = totalRecords;
        pager.PageSize = MoneyManagerConfig.AdminPageSize;

        messageLabel.Visible = success != 0;

        switch (success)
        {
            case 1:
                messageLabel.Text = Resources.Strings.AddCurrencySuccess;
                break;
            case 2:
                messageLabel.Text = Resources.Strings.UpdateCurrencySuccess;
                break;
            case 3:
                messageLabel.Text = Resources.Strings.DeleteCurrencySuccess;
                break;
            case -1:
                messageLabel.Text = Resources.Strings.AddAccountUnSuccess;
                break;
            case -2:
                messageLabel.Text = Resources.Strings.UpdateCurrencyUnsuccess;
                break;
            case -3:
                messageLabel.Text = string.Format(Resources.Strings.DeleteCurrencyUnsuccess, errorIds);
                break;

        }
    }

    #endregion

    #region public functions

    public string GetOrderByLink(string propertyName)
    {
        OrderDirection newOrderDirection = orderDirection;
        string url = CreateUrlParams();
        if (propertyName == orderBy.ToString())
        {
            newOrderDirection = orderDirection == OrderDirection.ASC ? OrderDirection.DESC : OrderDirection.ASC;
        }
        return String.Format("{0}{4}OrderBy={1}&OrderDirection={2}&Page={3}", url,
            propertyName, newOrderDirection.ToString(), pager.PageIndex, url.IndexOf('?') > 0 ? "&" : "?");
    }

    private string CreateUrlParams()
    {
        return redirectUrl;
    }

    public string GetOrderDirectionIndicator(string propertyName)
    {
        if (propertyName == orderBy.ToString())
        {
            return String.Format("<img src=\"../images/{0}.gif\" alt=\"{0}\" align=\"absmiddle\" />", orderDirection);
        }
        return "";
    }

    #endregion
}
